<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
class IndexController extends AdminController {
    public function _initialize(){
        parent::_initialize();
    }
    //空操作
    public function _empty(){
        header("HTTP/1.0 404 Not Found");
        $this->display('Public:404');
    }
    public function index(){
		$where['admin_id'] = $_SESSION['ADMIN_KEY'];
		$list = M('Admin')->where($where)->find();
		$this->assign('admin',$list);
        $this->display();
    }

  

    /**
     * 删除缓存方法
     */
    public function cache(){
        $cacheDir = $_POST['type'];
        $type = $cacheDir;
        //将传递过来的值进行切割，我是已“-”进行切割的
        $name = explode('-', $type);
        //得到切割的条数，便于下面循环
        $count = count($name);
        //循环调用上面的方法
        for ($i = 0; $i < $count; $i++)
        {
            //得到文件的绝对路径
            $abs_dir = dirname(dirname(dirname(dirname(__FILE__))));
            //组合路径
            $pa = $abs_dir . str_replace("/", "\\", str_replace("./", "\\", RUNTIME_PATH)); //得到运行时的目录
            $runtime = $pa . 'common~runtime.php';
            if (file_exists($runtime))//判断 文件是否存在
            {
                unlink($runtime); //进行文件删除
            }
            //调用删除文件夹下所有文件的方法
            $this->rmFile($pa, $name[$i]);
        }
        $data['status'] = 1;
        $data['info'] = "清理成功";
        $this->ajaxReturn($data);
    }
	public function infoStatistics(){
		$this->display();
	}
	
    /**
     * 删除文件和目录
     * @param type $path 要删除文件夹路径
     * @param type $fileName 要删除的目录名称
     */
    private function rmFile($path, $fileName)
    {//删除执行的方法
        //去除空格
        $path = preg_replace('/(\/){2,}|{\\\}{1,}/', '/', $path);
        //得到完整目录
        $path.= $fileName;
        //判断此文件是否为一个文件目录
        if (is_dir($path))
        {
            //打开文件
            if ($dh = opendir($path))
            {
                //遍历文件目录名称
                while (($file = readdir($dh)) != false)
                {
                    $sub_file_path = $path . "\\" . $file;
                    if ("." == $file || ".." == $file)
                    {
                        continue;
                    }
                    if (is_dir($sub_file_path))
                    {
                        $this->rmFile($sub_file_path, "");
                        rmdir($sub_file_path);
                    }
                    //逐一进行删除
                    unlink($sub_file_path);
                }
                //关闭文件
                closedir($dh);
            }
            rmdir($sub_file_path);//删除当前目录
        }
    }
	
	/**
	*修改密码
	*/
	public function savepass(){
		if(IS_POST){
			$password = I('post.password');
			$new_password = I('post.new_pwd');
			$repwd = I('post.repwd');
			if(empty($password)){
				$this->error('请输入原始密码');
			}
			if(empty($new_password)){
				$this->error('请输入新密码');
			}
			if(empty($repwd)){
				$this->error('请确认密码');
			}
			if($new_password != $repwd){
				$this->error('两次输入密码不同');
			}
			$where['admin_id'] = I('post.admin_id');
			$list = M('Admin')->where($where)->find();
			if($list['password'] != md5(trim($password))){
				$this->error('原始密码不正确');
			}
			$re = M('Admin')->where($where)->setField('password',md5(trim($new_password)));
			if($re){
				$this->success('修改成功',U('Index/infoStatistics'));
			}else{
				$this->error('修改失败');
			}
		}else{
			$where['admin_id'] = I('get.admin_id');
			$list = M('Admin')->where($where)->find();
			$this->assign('admin',$list);
			$this->display();
		}
	
	}
}